package cn.shujuhai.common.controller.common;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
public class GeneralPageController {
    @GetMapping(value = {"/page/{*path}", "/"})
    public String showPage(@PathVariable(value = "path", required = false) String path,
                           @RequestParam Map<String, String> params,
                           ModelMap result) {
        result.mergeAttributes(params);
        path = (path != null && !path.trim().isEmpty()) ? "page" + path : "redirect:/channel/zh-CN/index.html";
        return path;
    }

    @GetMapping(value = {"/vm/{*path}"})
    public String showPages(@PathVariable(value = "path", required = false) String path,
                            @RequestParam Map<String, String> params,
                            ModelMap result) {
        result.mergeAttributes(params);
        path = "vm" + path + ".html";
        return path;
    }

    /**
     * 处理非法请求
     *
     * @return 响应结果
     */
    @RequestMapping("*")
    public ResponseEntity<String> handleNotFound() {
        return new ResponseEntity<>("非法请求", HttpStatus.NOT_FOUND);
    }
}
